﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MYQQX
{
    public partial class ShowForm : Form
    {
        private CardGroup _group;

        // 是否开启选择（开启后，卡牌支持单选）
        public bool AllowChoose = false;
        // 选择的卡牌的名称（后续的操作使用名称，而非对应的卡）
        public string ChooseCard { get; private set; }
        private Label PreLabel { get; set; }

        public ShowForm(CardGroup group)
        {
            _group = group;
            InitializeComponent();
        }

        private void ShowForm_Load(object sender, EventArgs e)
        {
            lblScore.Text = $"总分：{_group.CurrentScore} 分";

            for (int i = 0; i < _group.cardList.Count; i++)
            {
                var card = _group.cardList[i];

                Label label = new Label();
                label.Text = "  " + GetCardName(card.Name);
                label.AutoSize = false;
                label.Width = 150;
                label.Height = 20;
                label.TextAlign = ContentAlignment.MiddleLeft;
                label.Tag = card;
                label.BackColor = card.Color;
                label.Click += card_click;

                Tips.SetToolTip(label, GroupConfig.Instance.GetTips(card.Name));

                var y = i / 4;
                var x = i % 4;

                label.Location = new Point(5 + x * 170, 5 + y * 25);

                pnlCard.Controls.Add(label);
            }

            for (int i = 0; i < _group.Group.Count; i++)
            {
                var gp = _group.Group[i];


                Label label = new Label();
                label.Text = $"  {gp.Name}[{gp.Score}]";
                label.AutoSize = false;
                label.Width = 150;
                label.Height = 20;
                label.TextAlign = ContentAlignment.MiddleLeft;
                label.BackColor = Color.Yellow;

                Tips.SetToolTip(label, string.Join("\r\n", gp.groups));

                var y = i / 4;
                var x = i % 4;

                label.Location = new Point(5 + x * 170, 5 + y * 25);

                pnlGroup.Controls.Add(label);
            }
        }

        private void card_click(object sender, EventArgs e)
        {
            if (AllowChoose)
            {
                if (PreLabel != null)
                    PreLabel.Text = "  " + GetCardName((PreLabel.Tag as Card).Name);

                var curLabel = sender as Label;
                var card = curLabel.Tag as Card;
                curLabel.Text = "[选择]" + GetCardName(card.Name);
                PreLabel = curLabel;
                ChooseCard = card.Name;
            }
        }

        private string GetCardName(string name)
        {
            var sp = _group.spList.FirstOrDefault(t => t.CardName == name);
            if (sp != null)
                return sp.CardName;

            return name;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if(AllowChoose && ChooseCard==null)
            {
                MessageBox.Show("请选择一张卡牌！");
                return;
            }

            this.DialogResult = DialogResult.OK;
        }

        private void btnSuggest_Click(object sender, EventArgs e)
        {
            Dictionary<string, SuggestInfo> sg = GroupConfig.Instance.Suggest(this._group);
            
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("推荐卡牌:");
            foreach (var key in sg.Keys)
            {
                var si = sg[key];

                sb.AppendLine(key + " => " + string.Join(",", si.gps.Select(g => $"{g.Name}[{g.Score}]")));
            }
            MessageBox.Show(sb.ToString());
        }

        private int NeedCout(ScoreGroup sp)
        {
            return sp.groups.Count() - sp.groups.Intersect(this._group.cardList.Select(t => t.Name)).Count();
        }

        private void ShowForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (AllowChoose && ChooseCard == null)
            {
                MessageBox.Show("请选择一张卡牌！");
                e.Cancel = true;
            }
        }
    }

    public class SuggestInfo
    {
        public string cName;

        public List<ScoreGroup> gps;

        public double sNum;
    }
}
